*************************************************************
*** RUN JACKKNIFE REGS COMPARING HOSP TO NBRS WITHIN 100M ***
*** BUT  CLUSTERING AT DIFF SEG LENGTHS         		  ***
*** Last edited: 1/19/2023 								  ***
*************************************************************
log using "${CodePath}/log/regs_hosp_100mradius_diffsegs_$S_DATE.log", text replace
*--------------------------------------------------
* Select and create dataset for regressions
*--------------------------------------------------

local hospvars    "log_n_claims"

use "${DataPath}/within100m/pnlist_groups_100m.dta", clear
duplicates drop hospcomp_group_100 pn, force
expand 9
sort pn
bys pn hospcomp_group_100 : gen fyear = _n + 2006
preserve
	use "${DataPath}/derived/hospyear_0716_jk.dta", clear
	keep pn fyear dist_to_border RACregion_audit* jk_RACregion_audit_in2011* jk_state_audit_in2011* state_audit_in2011* audit_in2011* jk_RACregion_audit_in2011* demandrate_in2011* jk_RACregion_demand_in2011*  jk_state_demand_in2011* `hospvars' nearseg* RACregion state
	keep if fyear >= 2007 & fyear <= 2015
	tempfile temp
	save `temp'
restore

merge m:1 pn fyear using `temp', keep(1 3) gen(_jkmerge)


egen hospcomp_group_fyear = group(hospcomp_group_100 fyear)
egen state_splitid = group(state split_id)
egen state_nearseg100 = group(state nearseg100_state)

*--------------------------------------------------
* Make event studies
*-------------------------------------------------- 

local segmentlengths = "50 150"
local hospvars "log_n_claims"

foreach seglength of local segmentlengths{
	foreach hospvar of local hospvars {
		reghdfe  `hospvar' jk_state_audit_in2011_X2*, absorb(pn hospcomp_group_fyear) cluster(nearseg`seglength'_state_2 state)
		event_study95 jk_state_audit_in2011
		graph export "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_100m/Event Studies/nearseg`seglength'_state_2__state/CI95/`hospvar'.png", replace

		ivreghdfe  `hospvar' (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
					= jk_state_audit_in2011_X2007 jk_state_audit_in2011_X2008 jk_state_audit_in2011_X2009 jk_state_audit_in2011_X2011 jk_state_audit_in2011_X2012 jk_state_audit_in2011_X2013 jk_state_audit_in2011_X2014 jk_state_audit_in2011_X2015) ///
				,absorb(hospcomp_group_fyear pn) cluster(state nearseg`seglength'_state_2)
		iv_event_study95_yscale audit_in2011 -0.04 0.02 0.02
		graph export "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_100m/Event Studies/nearseg`seglength'_state_2__state/CI95/`hospvar'_IV.png", replace

	}
}


log close




	